<?php

namespace app\index\controller;

use think\Controller;
use think\Request;

class Cart extends Base
{

    // 添加购物车方法
    public function addCart(Request $request){

        // 判断用户当前是否登录，
        if(!session("?index_user")){
            $this->error("对不起您还没有登录，请先登录。",'/index/Login/index');
            die;
        }
        if($request->isPost()){
            $title = "添加到购物车";

            // 获取提交表单中的所有数据
            $data = $request->param();

            // 表单验证
            $rule = [
                "goods_id" => 'require|number',
                'cart_number' => 'require|number|gt:0',
                'goods_attr_ids' => 'require'
            ];

            $validate = new \think\Validate($rule);

            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            // 添加到购物车
            \app\index\model\Cart::addCart($data['goods_id'],$data['cart_number'],$data['goods_attr_ids']);

            // 获取商品信息
            $goods = \app\index\model\Goods::where("id",$data['goods_id'])->find();

            // 获取商品类型信息
            $goods_attr = \app\index\model\GoodsAttr::getAttrByIds($data['goods_attr_ids']);

            return view("/cart/cartOk",[
                'title' => $title,
                'goods' => $goods,
                'goods_attr' => $goods_attr
            ]);
        }else{
            $this->error('请求方式不正确','index/index/index');
        }

    }

    // 查看购物车方法
    public function Cart(Request $request)
    {
        // 判断用户当前是否登录，
        if(!session("?index_user")){
            $this->error("对不起您还没有登录，请先登录。",'/index/Login/index');
            die;
        }

        $title = "购物车列表";

        // 当前用户的购物车列表
        $data = \app\index\model\Cart::getAllCart();

        # 消耗数据库资源的方法
        /*foreach($data as &$v)
        {
            $v['goods'] = \app\index\model\Goods::find($v['goods_id'])->toArray();
            $v['goods_attr'] = \app\index\model\GoodsAttr::getAttrByIds($v['goods_attr_ids']);
        }

        unset($v);*/

        // 优化查询
        // 取出所有的商品id
        $goods_ids = [];
        foreach($data as $v){
            $goods_ids[] = $v['goods_id'];
        }
        unset($v);

        // 数组去重
        $goods_ids = array_unique($goods_ids);

        // 根据id获取商品
        $goods = \app\index\model\Goods::where('id','in',$goods_ids)->select();
        $goods = (new \think\Collection($goods))->toArray();

        // 将数组商品id作为数组下表
        $new_goods = [];
        foreach($goods as &$v)
        {
            $new_goods[$v['id']] = $v;
        }

        unset($v);

        // 合并数组 将购物车数据 和商品信息关联上
        foreach($data as &$v){
            $v['goods'] = $new_goods[$v['goods_id']];
        }

        // 商品属性值
        $goods_attr_ids = [];
        foreach($data as &$v){

            $temp = explode(',',$v['goods_attr_ids']);
            $goods_attr_ids = array_merge($goods_attr_ids,$temp);
        }

        unset($v);

        // 数组去重
        $goods_attr_ids = array_unique($goods_attr_ids);

        // 获取属性值
        $goods_attr = \app\index\model\GoodsAttr::getAttrByIds($goods_attr_ids);
        $goods_attr = (new \think\Collection($goods_attr))->toArray();

        // 将商品id作为属性值数组的下标
        $new_goods_attr = [];
        foreach($goods_attr as $v){
            $new_goods_attr[$v['id']] = $v;
        }

        unset($v);
        // 合并数组，将购物车信息和 商品属性信息合并
        foreach($data as &$v){
            $v['goods_attr'] = [];

            $temp = explode(',',$v['goods_attr_ids']);

            foreach($temp as $id)
            {
                $v['goods_attr'][] = $new_goods_attr[$id];
            }
        }

        unset($v);

        return view("/cart/cart",[
            'title' => $title,
            'data' => $data,
        ]);
    }

    // ajax请求方法 修改购物车数量
    public function changenum(Request $request)
    {
        $data = $request->param();

        if(!session("?index_user")){
            $this->error("对不起您还没有登录，请先登录。",'/index/Login/index');
            die;
        }

        $user_id = session("index_user.id");

        $where = [
            'goods_id' => $data['goods_id'],
            'goods_attr_ids' => $data['goods_attr_ids'],
            'user_id' => $user_id
        ];

        \app\index\model\Cart::where($where)->update(['cart_number'=>$data['cart_number']]);

        $res = [
            'code' => 10000,
            'msg' => "success"
        ];

        return json($res);
    }

    public function delete(Request $request)
    {
        if(!session("?index_user")){
            $this->error("对不起您还没有登录，请先登录。",'/index/Login/index');
            die;
        }
        $data = $request->param();

        $rule = [
            'goods_id|商品id' => 'require|number',
            'goods_attr_ids|商品属性id' => 'require',
        ];

        $validate = new \think\Validate($rule);

        if(!$validate->check($data))
        {
            $this->error($validate->getError());
            die;
        }

        $user_id = session("index_user.id");

        $result = \app\index\model\Cart::where(['user_id'=>$user_id,'goods_id'=>$data['goods_id'],'goods_attr_ids'=>$data['goods_attr_ids']])->delete();

        if($result)
        {
            $res = [
                'code' => 10000,
                'msg' => "success"
            ];

            return json($res);
        }else{
            $res = [
                'code' => 10001,
                'msg' => "error"
            ];

            return json($res);
        }
    }
}
